package com.phonemap.dto;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.json.JSONObject;

import com.google.appengine.api.datastore.GeoPt;
import com.google.appengine.api.search.GeoPoint;
import com.phonemap.common.AppConstant;
import com.phonemap.math.LatLng;
import com.phonemap.models.Position;

/**
 * The Class PositionDTO.
 */
public class PositionDTO implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The position_id. */
	private long positionId;

	/** The user_id. */
	private long userId;

	/** The located. */
	private GeoPoint located;
	
	/** The position. */
	private LatLng position;

	/** The located_time. */
	private Date locatedTime;

	/**
	 * Instantiates a new position dto.
	 */
	public PositionDTO() {

	}

	/**
	 * Instantiates a new position dto.
	 * 
	 * @param positionId
	 *            the position id
	 * @param userId
	 *            the use id
	 * @param located
	 *            the located
	 * @param locatedTime
	 *            the located time
	 */
	public PositionDTO(long positionId, long userId, GeoPoint located,
			Date locatedTime) {
		this.positionId = positionId;
		this.userId = userId;
		this.located = located;
		this.locatedTime = locatedTime;
	}
	
	/**
	 * Instantiates a new position dto.
	 *
	 * @param json the json
	 * @throws Exception the exception
	 */
	public PositionDTO(JSONObject json) throws Exception{
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateFormat.setTimeZone(TimeZone.getTimeZone(AppConstant.TIME_ZONE));
			this.userId = json.getLong("user_id");
			double latitude = json.getDouble("latitude");
			double longitude = json.getDouble("longitude");
			this.located = new GeoPoint(latitude, longitude);
			this.position = new LatLng(latitude, longitude);
			this.locatedTime = dateFormat.parse(json.getString("located_time"));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
		
	}

	/**
	 * Gets the position id.
	 * 
	 * @return the position id
	 */
	public long getPositionId() {
		return this.positionId;
	}

	/**
	 * Sets the position id.
	 * 
	 * @param positionId
	 *            the new position id
	 */
	public void setPositionId(long positionId) {
		this.positionId = positionId;
	}

	/**
	 * Gets the use id.
	 * 
	 * @return the use id
	 */
	public long getUserId() {
		return this.userId;
	}

	/**
	 * Sets the use id.
	 * 
	 * @param userId
	 *            the new use id
	 */
	public void setUserId(long userId) {
		this.userId = userId;
	}

	/**
	 * Gets the located.
	 * 
	 * @return the located
	 */
	public GeoPoint getLocated() {
		return this.located;
	}

	/**
	 * Sets the located.
	 * 
	 * @param located
	 *            the new located
	 */
	public void setLocated(GeoPoint located) {
		this.located = located;
	}

	/**
	 * Gets the located time.
	 * 
	 * @return the located time
	 */
	public Date getLocatedTime() {
		return this.locatedTime;
	}

	/**
	 * Sets the located time.
	 * 
	 * @param locatedTime
	 *            the new located time
	 */
	public void setLocatedTime(Date locatedTime) {
		this.locatedTime = locatedTime;
	}
	
	/**
	 * Gets the position.
	 *
	 * @return the position
	 */
	public LatLng getPosition() {
		return this.position;
	}

	public void setPosition(LatLng position) {
		this.position = position;
	}

	/**
	 * To String.
	 *
	 * @return the string
	 */
	@Override
	public String toString() {
		return "{\"name\":\"" + this.positionId + "\", \"user_id\":\"" + this.userId
				+ "\", \"located\":\"" + this.located.getLatitude() + ","
				+ this.located.getLongitude() + "\", \"located_time\":\""
				+ this.locatedTime + "\"}";
	}

}
